package com.ydsungan.web;
import com.ydsungan.bean.*;
import com.ydsungan.service.ClockinService;
import com.ydsungan.service.SigninService;
import com.ydsungan.service.StudentService;
import com.ydsungan.service.TeacherService;
import com.ydsungan.serviceimpl.ClockinServiceImpl;
import com.ydsungan.serviceimpl.SigninServiceImpl;
import com.ydsungan.serviceimpl.StudentServiceImpl;
import com.ydsungan.serviceimpl.TeacherServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DetailServlet extends HttpServlet {

    private StudentService studentService = null;
    private SigninService signinService = null;
    private ClockinService clockinService = null;
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public void init() throws ServletException {
        try {
            studentService = new StudentServiceImpl();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            signinService = new SigninServiceImpl();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            clockinService = new ClockinServiceImpl();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");

        //今天的签到有那些学生签了
        //获取今天日期，获取老师的organiser:id，以id和日期去获取signin表，
        //拿到signin的id，去clockin表查找signin = id的行，就是要获取的行

        String today = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = new Date();
            today = sdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Teacher teacher = (Teacher)this.getServletContext().getAttribute("loginteacher");
        Map map = new HashMap();
        map.put("signinDay", today);
        map.put("organiser", teacher.getId());

        logger.info("today:" + today);

        List<Signin> signinList = signinService.getSigninByMap(map);
        Signin signin = signinList.get(0);
        logger.info("signid="+signin.getId());

        Map map2 = new HashMap();
        map2.put("signin", signin.getId());
        List<Clockin> clockinList = clockinService.findClockinByMap(map2);
        this.getServletContext().setAttribute("has_clockined_num", clockinList.size());
        List<Result> resultList = new ArrayList<Result>();
        //循环clockinList,按照学好，去学生表找学生，获取学生的详细信息
        for(Clockin clockin : clockinList){
            logger.info("clockin的day="+clockin.getDay());
            String studentId = clockin.getStudent();
            Map map3 = new HashMap();
            map3.put("id",studentId);
            Student student = studentService.findStudentByMap(map3);

            Result result = new Result();
            result.setId(studentId);
            result.setName(student.getName());
            result.setClazz(student.getClazz());
            result.setGender(student.getGender());

            result.setSigninTime(clockin.getDate().split(" ")[1]);
            result.setAddress(clockin.getAddress());


            result.setContact(clockin.getContact() == 0 ? "未和病例接触" : "有和病例接触");
            result.setTemperature(clockin.getTemprature());

            result.setTemperatureDescription( Double.parseDouble(clockin.getTemprature()) > 37.4 ? "体温较高" : "体温正常");
            result.setTemperatureJudge(Double.parseDouble(clockin.getTemprature()) > 37.4 ? 0 : 1);

            result.setContactDescription(clockin.getContact() == 0 ? "无感染风险" : "建议隔离");
            result.setContactJudge(clockin.getContact() == 0 ? 1 : 0);

            //获取昨天日期，和该学好，查clockin表里面的地址
            String yesterday = "";
            try {
                Date date = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24);
                yesterday = sdf.format(date);
            } catch (Exception e) {
                e.printStackTrace();
            }
            logger.info("yes" + yesterday);
            Map map4 = new HashMap();
            map4.put("student",studentId);
            map4.put("day",yesterday);
            List<Clockin> clockinList2 = clockinService.findClockinByMap(map4);
            if(clockinList2.size() == 0){
                result.setAddressJudge(1);
                result.setAddressDescription("无城际间移动");
            }else{
                Clockin clockin2 = clockinList2.get(0);
                logger.info(student.getName() +  clockin.getAddress() + " " + clockin2.getAddress());

                if(clockin2.getAddress().equals(clockin.getAddress())){
                    result.setAddressJudge(1);
                    result.setAddressDescription("无城际间移动");
                }else{
                    result.setAddressJudge(0);
                    result.setAddressDescription("城际移动");
                }
            }
            resultList.add(result);
        }
        this.getServletContext().setAttribute("resultList",resultList);
        response.sendRedirect("detail.jsp");
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

}
